package com.lv.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lv.common.AjaxResult;
import com.lv.common.Messages;
import com.lv.common.dto.GoodDto;
import com.lv.entity.Good;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-21
 */
@RestController
@RequestMapping("/good")
public class GoodController extends BaseController {

    @PostMapping("/getAllGood")
    public AjaxResult getAllGood(){
        List<Good> allGood =goodMapper.getAllGood();
        return AjaxResult.success(allGood);
    }
    @PostMapping("/getGoods")
    public AjaxResult getGoods(){
        List<GoodDto> goods = goodMapper.getGoods();
        return AjaxResult.success(goods);
    }
    @PostMapping("/getGood/{name}")
    public AjaxResult getGood(@PathVariable String name){
        List<Good> goods = goodService.list(new QueryWrapper<Good>().like("good_name", name));
        return AjaxResult.success(goods);
    }
    @PostMapping("/getGoodById/{id}")
    public AjaxResult getGoodById(@PathVariable Long id){
        Good good = goodService.getById(id);
        return AjaxResult.success(good);
    }

    @PostMapping("/saveGood")
    public AjaxResult addGood( Good good){
        boolean b = goodService.save(good);
        if(b){
            return AjaxResult.success(Messages.SAVE_SUCCESS);
        }
        return AjaxResult.error(Messages.SAVE_ERROR);
    }
    @PostMapping("/deleteGood/{id}")
    public AjaxResult deleteGood(@PathVariable Long id){
        boolean b = goodService.removeById(id);
        if(b){
            return AjaxResult.success(Messages.REMOVE_SUCCESS);
        }
        return AjaxResult.error(Messages.REMOVE_ERROR);
    }

    @PostMapping("/updateGood")
    public AjaxResult updateGood( Good good){
        boolean b = goodService.updateById(good);
        if(!b){
            return AjaxResult.error(Messages.UPDATE_ERROR);
        }
        return AjaxResult.success(Messages.UPDATE_SUCCESS);
    }
    @PostMapping("/deleteGoodByIds")
    public AjaxResult deleteGoodByIds(String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        boolean b = goodService.removeByIds(list);
        if(!b){
            return AjaxResult.error(Messages.REMOVE_ERROR);
        }
        return AjaxResult.success(Messages.REMOVE_SUCCESS);
    }
    @PostMapping("/createOrder")
    public AjaxResult createOrder(String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        List<Good> goods = goodService.list(new QueryWrapper<Good>().in("good_id", list));
        return AjaxResult.success(goods);
    }

    @PostMapping("/getPriceById/{id}")
    public AjaxResult getPriceById(@PathVariable Long id){
        Good good = goodService.getById(id);
        return AjaxResult.success(good.getPrice());
    }
}

